package com.shop.service.module.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.service.module.entity.OrderEntity;
import com.shop.service.module.entity.Result;
import com.shop.service.module.mapper.OrderMapper;
import com.shop.service.module.service.OrderService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements OrderService {
    @Override
    public Result getListPage(Integer pno, Integer psize, Integer status, String orderNo, Integer type, String phone) {
        Page<OrderEntity> page = new Page(pno, psize);
        QueryWrapper<OrderEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(orderNo)) {
            queryWrapper.like("orderNo", orderNo);
        }
        if (!StringUtils.isEmpty(type)) {
            queryWrapper.eq("type", type);
        }
        if (!StringUtils.isEmpty(phone)) {
            queryWrapper.like("phone", phone);
        }

        Page<OrderEntity> result = baseMapper.selectPage(page, queryWrapper);
        JSONObject data = new JSONObject();
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("pno", pno);
        jsonResult.put("psize", psize);
        jsonResult.put("pCount", result.getPages());
        jsonResult.put("totalElements", result.getTotal());
        data.put("list", result.getRecords());
        data.put("page", jsonResult);
        return Result.end(200, data, "查询成功");


    }

    @Override
    public Result sendOrder(Long id, String postCode) {
        //根据id查询出订单详情
        OrderEntity orderEntity = baseMapper.selectById(id);
        orderEntity.setStatus(2);
        orderEntity.setExpressNo(postCode);
        baseMapper.updateById(orderEntity);
        return Result.end(200, "", "更新成功");
    }
}
